package com.xinhe.web.service;

import com.xinhe.web.common.Result;
import com.xinhe.web.entity.Attach;
import com.xinhe.web.reposity.AttachRepository;
import com.xinhe.web.response.AttachView;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class AttachService {
    @Autowired
    private AttachRepository attachRepository;
    public List<AttachView> findByBusinessId(Long businessId) {
        Set<Long> businessIds = new HashSet<>();
        businessIds.add(businessId);
        List<AttachView> views = attachRepository.pageAttach(businessIds);
        return views;
    }
    public Result<List<AttachView>> findByBusinessIdAndType(Long businessId, String type) {
        Set<Long> businessIds = new HashSet<>();
        businessIds.add(businessId);
        List<AttachView> views = attachRepository.pageAttach(businessIds,type);
        return Result.ok(views);
    }
    @Transactional
    public Result updateAttachByBusinessIdAndAttachIds(Long businessId, Set<Long> attachIds) {
        if (CollectionUtils.isNotEmpty(attachIds)) {
            Map<Long, Long> attachIdMap = new HashMap<>();
            for (Long attachId : attachIds) {
                attachIdMap.put(attachId, attachId);
            }
            List<Attach> attachList = attachRepository.findByBusinessId(businessId);
            for (Attach attach : attachList) {
                Long attachId = attachIdMap.get(attach.getId());
                if (null == attachId) {
                    attachRepository.deleteById(attach.getId());
                }
            }
            attachRepository.updateAttachByBusinessIdAndAttachIds(businessId, attachIds);
        }
        return Result.ok();
    }

    /*public ListResult<AttachView> findByBusinessIdInAndType(AttachBusinessIdsAndTypeQuery query) {
        List<AttachView> views = this.attachDao.pageAttach(new AttachQuery(query.getBusinessIds(), query.getType()));
        return new ListResult(views);
    }

    public ListResult<AttachView> findByBusinessIdIn(AttachBusinessIdsQuery query) {
        List<AttachView> views = this.attachDao.pageAttach(new AttachQuery(query.getBusinessIds()));
        return new ListResult(views);
    }

    @Transactional
    public ListResult<AttachView> updateAttachSingleBusiness(AttachSingleBusinessUpdateModel model) {
        this.attachDao.deleteByBusinessId(model.getBusinessId());
        List<AttachView> views = new ArrayList<>();
        Set<Long> attachIds = new HashSet<>();
        Map<Long, AttachItemModel> attachMap = new HashMap<>();
        for (AttachItemModel temp : model.getAttaches()) {
            attachIds.add(temp.getAttachId());
            attachMap.put(temp.getAttachId(), temp);
        }
        Set<Attach> updateAttachs = new HashSet<>();
        List<Attach> attachs = this.attachDao.findByIdIn(attachIds);
        AttachItemModel attachModel = null;
        for (Attach attach : attachs) {
            attachModel = attachMap.get(attach.getId());
            attach.setRemark(attachModel.getRemark());
            attach.setType(attachModel.getType());
            attach.setRemoved(Integer.valueOf(0));
            attach.setBusinessId(model.getBusinessId());
            updateAttachs.add(attach);
            views.add(attach.convertToView());
        }
        this.attachDao.update(updateAttachs);
        return new ListResult(views);
    }

    @Transactional
    public BaseResult updateAttachBatchBusiness(AttachBatchBusinessUpdateModel model) {
        this.attachDao.deleteByBusinessIdIn(model.getBusinessIds());
        Set<Long> attachIds = new HashSet<>();
        Map<Long, AttachItemModel> attachMap = new HashMap<>();
        for (AttachItemModel temp : model.getAttaches()) {
            attachIds.add(temp.getAttachId());
            attachMap.put(temp.getAttachId(), temp);
        }
        Set<Attach> saveAttachs = new HashSet<>();
        Set<Attach> updateAttachs = new HashSet<>();
        List<Attach> attachs = this.attachDao.findByIdInAndIgnoreRemoved(attachIds);
        AttachItemModel attachModel = null;
        for (Attach attach : attachs) {
            int i = 0;
            for (Long businessId : model.getBusinessIds()) {
                attachModel = attachMap.get(attach.getId());
                attach.setRemark(attachModel.getRemark());
                attach.setType(attachModel.getType());
                attach.setRemoved(Integer.valueOf(0));
                if (0 == i) {
                    attach.setBusinessId(businessId);
                    updateAttachs.add(attach);
                    i++; continue;
                }
                Attach a = new Attach();
                BeanUtils.copyProperties(attach, a);
                a.setBusinessId(businessId);
                saveAttachs.add(a);
            }
        }

        this.attachDao.update(updateAttachs);
        if (CollectionUtils.isNotEmpty(saveAttachs)) {
            this.attachDao.save(saveAttachs);
        }
        return new BaseResult();
    }


    @Transactional
    public BaseResult deleteByBusinessId(Long businessId) {
        this.attachDao.deleteByBusinessId(businessId);
        return new BaseResult();
    }

    @Transactional
    public BeanResult<AttachView> saveAttachTemp(AttachTempModel model) {
        Attach attach = new Attach();
        attach.setContentType(model.getContentType());
        attach.setName(model.getName());
        attach.setPath(model.getPath());
        attach.setType(model.getType());
        attach.setRemoved(Integer.valueOf(-1));
        this.attachDao.save((BaseEntityImpl)attach);
        AttachView view = new AttachView();
        BeanUtils.copyProperties(attach, view);
        return new BeanResult(view);
    }

    public PageResult<AttachView> pageAttach(AttachQuery query) {
        Integer total = Integer.valueOf(0);
        if (0 < query.getLimit().intValue()) {
            total = this.attachDao.countAttach(query);
        }
        List<AttachView> views = null;
        if (0 < total.intValue()) {
            views = this.attachDao.pageAttach(query);
        }
        return new PageResult(total, views);
    }

    protected IBaseDao<Attach> getRealDao() {
        return (IBaseDao<Attach>)this.attachDao;
    }

    public ListResult<AttachView> findByIdIn(AttachIdsQuery query) {
        List<Attach> attachList = this.attachDao.findByIds(query.getIds());
        List<AttachView> viewList = new ArrayList<>();
        for (Attach attach : attachList) {
            AttachView attachView = new AttachView();
            copyPropertiesIgnoreNull(attach, attachView);
            viewList.add(attachView);
        }
        return new ListResult(viewList);
    }

    public BeanResult<AttachView> findOneById(Long id) {
        Attach attach = (Attach)this.attachDao.findById(id);
        AttachView attachView = new AttachView();
        copyPropertiesIgnoreNull(attach, attachView);
        return new BeanResult(attachView);
    }

    @Transactional
    public BaseResult updateAttachByBusinessIdAndPath(Long businessId, String path) {
        Attach attach = this.attachDao.findOneByPath(path);
        attach.setBusinessId(businessId);
        attach.setRemoved(Integer.valueOf(0));
        this.attachDao.update((BaseEntityImpl)attach);
        return new BaseResult();
    }

    @Transactional
    public BeanResult<Set<Long>> copyAttachIds(Set<Long> attachIds) {
        List<Attach> attachViews = this.attachDao.findByAttachIds(attachIds);
        Set<Long> ids = new HashSet<>();
        Iterator<Attach> var4 = attachViews.iterator();

        while (var4.hasNext()) {
            Attach attachView = var4.next();
            AttachView item = (AttachView)saveAttachTemp(new AttachTempModel(attachView.getPath(), attachView.getName(), attachView.getContentType(), attachView.getType())).getItem();
            ids.add(item.getId());
        }
        return new BeanResult(ids);
    }*/
}
